// pages/qiye_jieshao/qiye_jieshao.js
const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
    data: {
        navIndex: 0,
        scenicBoxTop: 0,
        scrollTop: 0,
        loadingType: 0,
        contentText: {
            contentdown: "上拉显示更多",
            contentrefresh: "正在加载...",
            contentnomore: "没有更多数据了"
        },
        userinfo: [],
        info: [],
        longitude: 0,
        latitude: 0,
        userlist: [],
        shoplist: [],
        shoplist1: [],
        p: 1
    },

    onPageScroll(e) {
        this.setData({
            scrollTop: e.scrollTop
        });
    },

    onLoad() {
        const userinfo = wx.getStorageSync('user');
        const longitude = wx.getStorageSync('userlon');
        const latitude = wx.getStorageSync('userlat');
        if (!longitude || !latitude) {
            this.choose_location();
        } else {
            this.setData({
                userinfo,
                longitude,
                latitude
            });
            this.getinfo();
            this.getshoplist();
        }
    },

    choose_location() {
        wx.showLoading({
            title: '定位中...'
        });
        wx.getLocation({
            type: 'gcj02',
            success: (res) => {
                wx.hideLoading();
                const latitude = res.latitude;
                const longitude = res.longitude;
                wx.setStorageSync('userlon', longitude);
                wx.setStorageSync('userlat', latitude);
                this.setData({
                    longitude,
                    latitude
                });
                this.getinfo();
                this.getshoplist();
            }
        });
    },

    getuserlist() {
        const that = this;
        util.httpRrquest(app.data.apiurl + 'shop/getMyAreaList', 'GET', {})
        .then(function (response) {  
            that.setData({
                userlist: response.data.data
            });
        }, function (error) {
            console.log(error);
        });
        
    },

    copynum(e) {
        const str = e.currentTarget.dataset.code;
        wx.setClipboardData({
            data: str,
            success() {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none'
                });
            }
        });
    },

    getshoplist() {
        const that = this;
        const longitude = wx.getStorageSync('userlon');
        const latitude = wx.getStorageSync('userlat');

        util.httpRrquest(app.data.apiurl + 'shop/getMyAreaShop', 'GET', {userlon: longitude,
                userlat: latitude})
        .then(function (response) {  
            var data = response.data.data;
            if (data.has == 1) {
                that.setData({
                    shoplist: data.data
                });
            }
        }, function (error) {
            console.log(error);
        });

        
    },

    getshoplist2() {
        const that = this;
        const longitude = wx.getStorageSync('userlon');
        const latitude = wx.getStorageSync('userlat');
        util.httpRrquest(app.data.apiurl + 'shop/getMyAreaChildShop', 'GET', {userlon: longitude,
                userlat: latitude})
        .then(function (response) {  
            var data = response.data.data;
            if (data.has == 1) {
                that.setData({
                    shoplist1: data.data
                });
            }
        }, function (error) {
            console.log(error);
        });
        
    },

    getinfo() {
        const that = this;
        util.httpRrquest(app.data.apiurl + 'shop/getMyArea', 'GET', {})
        .then(function (response) {  
            var data = response.data.data;
            if (data.has == 1) {
                that.setData({
                    info: data.data
                });
            }
        }, function (error) {
            console.log(error);
        });
    },

    jump() {
        wx.navigateTo({
            url: '/pages/commissionList/commissionList?type=2'
        });
    },

    navtab(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            navIndex: index,
            p: 1,
            shoplist1: [],
            shoplist: []
        });
        if (index == 0) {
            this.getshoplist();
        } else if (index == 1) {
            this.getshoplist2();
        } else {
            this.getuserlist();
        }
    },

    onReachBottom() {
        if (this.data.loadingType !== 0) {
            return;
        }
        this.setData({
            loadingType: 0
        });
    }
});